<?php
namespace Home\Controller;
use Home\Controller\UserController;
use Common\Model\FinanceModel;
use Common\Api\UserMoneyApi;
use Common\Model\UserRelationModel;
use Common\Model\MemberModel;
class ProjectController extends UserController {
	public function _initialize(){

		parent::_initialize();
	}
	// public function ceshi(){
    // $list = M('Project_log p')
    // ->join('yang_project_dongjie2 p2 on p2.project_id = p.id')
    // ->select();
    // foreach ($list as $k=>$v){
        // $project_log_id = $v['project_id'];
        // //直推上线
        // $USER = new MemberModel();
        // $user= M("Member")->where(['member_id'=>$v['uid']])->find();
        // if($user['pid']){
            // $up_user = $USER->getUserByUid($user['pid']);
             
            // if($up_user){
                // $price = $this->getPrice()['price'.$v['type']];
                // //dump($price);
                // $usd_price = $price*$v['money'];
                // //团队奖
                // $this->runDaishuMoney($up_user,$v['money'],1,$user['username'],$project_log_id);
                //加上线业绩
                // $this->addTeamYeji($user,$usd_price);
            // }
        // }
    // }
// }
	//获取智能收益
	public function getCurrencyType(){
	     $project = M('Project p')
	     ->where(['p.uid'=>$this->uid])
	     ->order('id asc')
	     ->join('join yang_currency1 c on p.type = c.currency_id')
	     ->select();
	     $return['data'] = $project;
	     $return['status'] = 1;
	     $this->ajaxReturn($return);
	}
	public function getUserMoneyNumByType(){
	    $type = I('type')?I('type'):'';
	    $list = M('Member_money')->where(['member_id'=>$this->uid])->find()['money'.$type];
	    $return['data'] = $list;
	     $return['status'] = 1;
	     $this->ajaxReturn($return);
	}
	//获取可以开启的币种
	public function getOpenCurrencyType(){
	    $project = M('currency1')
	    ->where(['switch'=>1])
	    ->select();
	    $return['data'] = $project;
	    $return['status'] = 1;
	    $this->ajaxReturn($return);
	}
	//获取币种详情
	public function getCurrencyId(){
	    $currency_id = I('currency_id');
	    $project = M('currency1')
	    ->where(['currency_id'=>$currency_id])
	    ->find();
	    $return['data'] = $project;
	    $return['status'] = 1;
	    $this->ajaxReturn($return);
	}
	//获取启动记录
	public function getOpenLog(){
	    $list = M('Project_log pl')
	    ->where(['pl.uid'=>$this->uid])
	    ->join('join yang_currency1 c on pl.type = c.currency_id')
	    ->order('id asc')
	    ->select();
	    $return['data'] = $list;
	    $return['status'] = 1;
	    $this->ajaxReturn($return);
	}
//获取今日智能收益
    public function getTodayProfit(){
        $table = $this->getMemberGroup(intval($this->uid));
        $today = strtotime(date("Y-m-d"),time());//零点时间
        //分红
        $where_fenhong['member_id'] = $this->uid;
        $where_fenhong['currency_id'] = 11;
        $where_fenhong['money_type'] = 1;
        $where_fenhong['type'] = 1;
        $where_fenhong['add_time'] = array('gt',$today);
        $fenhong = M('Finance_'.$table)
        ->where($where_fenhong)
        ->sum('money');
        //团队
        $where_tuandui['member_id'] = $this->uid;
        $where_tuandui['currency_id'] = 22;
        $where_tuandui['money_type'] = 1;
        $where_tuandui['type'] = 1;
        $where_tuandui['add_time'] = array('gt',$today);
        $tuandui = M('Finance_'.$table)
        ->where($where_tuandui)
        ->sum('money');
        //社区
        $where_shequ['member_id'] = $this->uid;
        $where_shequ['currency_id'] = 33;
        $where_shequ['money_type'] = 1;
        $where_shequ['type'] = 1;
        $where_shequ['add_time'] = array('gt',$today);
        $shequ = M('Finance_'.$table)
        ->where($where_shequ)
        ->sum('money');
    
        //总分红
        $where_fenhong_zong['member_id'] = $this->uid;
        $where_fenhong_zong['currency_id'] = 11;
        $where_fenhong_zong['money_type'] = 1;
        $where_fenhong_zong['type'] = 1;
        $fenhong_zong = M('Finance_'.$table)
        ->where($where_fenhong_zong)
        ->sum('money');
        //总团队
        $where_tuandui_zong['member_id'] = $this->uid;
        $where_tuandui_zong['currency_id'] = 22;
        $where_tuandui_zong['money_type'] = 1;
        $where_tuandui_zong['type'] = 1;
        $tuandui_zong = M('Finance_'.$table)
        ->where($where_tuandui_zong)
        ->sum('money');
        //总社区
        $where_shequ_zong['member_id'] = $this->uid;
        $where_shequ_zong['currency_id'] = 33;
        $where_shequ_zong['money_type'] = 1;
        $where_shequ_zong['type'] = 1;
        $shequ_zong = M('Finance_'.$table)
        ->where($where_shequ_zong)
        ->sum('money');
    
        //总收益(usd)
        $zongshouyi = $shequ_zong+$tuandui_zong+$fenhong_zong;
         
        $return['fenhong'] = $fenhong?$fenhong:0.0000;
        $return['tuandui'] = $tuandui?$tuandui:0.0000;
        $return['shequ'] = $shequ?$shequ:0.0000;
        $return['zongshouyi_jinri'] = $return['fenhong']+$return['tuandui']+$return['shequ'];
        $return['zongshouyi'] = $zongshouyi;
        $return['zongshouyi_usd'] = $zongshouyi*$this->getConfig()['vic_price'];
        $return['status'] = 1;
        $this->ajaxReturn($return);
    }
	//开启智能收益
	public function open(){
	    $num = I('num');
	    $type = I('type');
	    $tradepwd = I('tradepwd');
	    $project_id = I('id');
	    if($num<0){
	        $data['status']=-15;
	        $data['info']='请输入正确的数量';
	        $this->ajaxReturn($data);
	    }
	    if(md5($tradepwd)!=$this->user['pwdtrade']){
	        $data['status']=-3;
	        $data['info']='交易密码不正确';
	        $this->ajaxReturn($data);
	    }
	    $usermoney = M('Member_money')->where(['member_id'=>$this->uid])->find()['money'.$type];
	    if($usermoney<$num){
	        $data['status']=-3;
	        $data['info']='余额不足';
	        $this->ajaxReturn($data);
	    }
	    /* $currency = M('Currency1')->where(['currency_id'=>$type])->find()['currency_mark'];
	    if($type==1){
    	   $price = $this->getConfig()['vic_price'];
	    }else{
	        //币最新价
	        $url="https://api.huobi.pro/market/detail/merged?symbol=".$currency."usdt";
	        $result=$this->curl_get($url);
	        $price=$result['tick']['close'];
	        
	    } */
	    $price = $this->getPrice()['price'.$type];
		//dump($price);
	    $usd_price = $price*$num;
	    $project_log = M('Project_log')->where(['uid'=>$this->uid])->find();
	    if(!$project_log){
	        if($usd_price<300){
	            $data['status']=-3;
	            $data['info']='首次开启资产必须大于300美金';
	            $this->ajaxReturn($data);
	        }
	    }
	    //开启事物
	    M()->startTrans();
	    $r[] = M('Member_money')->where(['member_id'=>$this->uid])->setDec('money'.$type,$num);
	    //自动程序记录
	    $project = M('Project')->where(['id'=>$project_id])->find();
	    if($project['status']==0){
	        $r[] = M('Project')->where(['id'=>$project_id])->setField('status',1);
	    }
	    $project_log_id = $this->addProject($this->uid,$project_id,$type,$num);
	    //直推上线
	    $USER = new MemberModel();
		if($this->user['pid']){
		$up_user = $USER->getUserByUid($this->user['pid']);
	    if($up_user){
	        //第一次开启
	        if($project_log==NULL){
	            $USERRELATION = new UserRelationModel();
	            //加上线直推数量
	            $r[] = $USERRELATION->addZhituiNum($up_user['member_id'],1);
	            //加团队人数
	            $this->addTeamNum($this->user);
	        }
	        //团队奖
	        $this->runDaishuMoney($up_user,$num,1,$this->user['username'],$project_log_id,$type);
	        //加上线业绩
	        $this->addTeamYeji($this->user,$usd_price);
	    }
		}
	    if (in_array(false, $r)){
	        M()->rollback();
	        $data['status']=-3;
	        $data['info']='系统繁忙';
	        $this->ajaxReturn($data);
	    }else{
	        M()->commit();
	        $data['status']=1;
	        $data['info']='操作成功';
	        $this->ajaxReturn($data);
	    }
	}
	//转换后自动开启智能收益
	public function autoOpen($num,$type=1,$qufen){
	    $project_id = M('Project')->where(['type'=>$type,'uid'=>$this->uid])->find()['id'];
	    if($num<0){
	        $data['status']=-15;
	        $data['info']="参数错误，金额小于0";
	        $this->ajaxReturn($data);
	    }
	    $usermoney = M('Member_money')->where(['member_id'=>$this->uid])->find()['money'.$type];
	    if($usermoney<$num){
	        $data['status']=-3;
	        $data['info']='余额不足';
	        $this->ajaxReturn($data);
	    }
	    /* $price = $this->getConfig()['vic_price'];
	    $usd_price = $price*$num; */
	    $project_log = M('Project_log')->where(['uid'=>$this->uid])->find();
	    //开启事物
	    M()->startTrans();
	    $r[] = M('Member_money')->where(['member_id'=>$this->uid])->setDec('money'.$type,$num);
	    //自动程序记录
	    /* $project = M('Project')->where(['id'=>$project_id])->find();
	    if($project['status']==0){
	        $r[] = M('Project')->where(['id'=>$project_id])->setField('status',1);
	    } */
	    $project_log_id = $this->addProject($this->uid,$project_id,$type,$num);
		if($qufen==1){
	    //直推上线
	    $USER = new MemberModel();
	    if($this->user['pid']){
	        $up_user = $USER->getUserByUid($this->user['pid']);
	        if($up_user){	           
	            //团队奖
	            $this->runDaishuMoney($up_user,$num,1,$this->user['username'],$project_log_id,$type);
	            //加上线业绩
	            //$this->addTeamYeji($this->user,$usd_price);
	        }
	    }
		}
	    if (in_array(false, $r)){
	        M()->rollback();
	        $data['status']=-3;
	        $data['info']='系统繁忙';
	        $this->ajaxReturn($data);
	    }else{
	        M()->commit();
	        $data['status']=1;
	        $data['info']='操作成功';
	        $this->ajaxReturn($data);
	    }
	}
	//关闭智能收益
	public function close(){
	  $id = I('id'); 
	  $tradepwd = I('tradepwd');
	  if(empty($tradepwd)){
	      $data['status']=-1;
	      $data['info']='请输入交易密码';
	      $this->ajaxReturn($data);
	  }
	  $list = M('Project_log')->where(['id'=>$id])->find();
	  if(!$list){
	      $data['status']=-2;
	      $data['info']='参数错误';
	      $this->ajaxReturn($data);
	  }
	  if(md5($tradepwd)!=$this->user['pwdtrade']){
	      $data['status']=-3;
	      $data['info']='交易密码不正确';
	      $this->ajaxReturn($data);
	  }
	  //冻结期27天
	  if(time()<$list['add_time']+(35*24*3600)){
	    $fanhuan_money =  $list['money']-($list['money']*0.05);
	  }else{
	    $fanhuan_money =  $list['money']-($list['money']*0.01);
	  }
	  $API_USERMONEYLOGIC = new UserMoneyApi();
	  //扣钱
	  //减上线业绩
	  /* if($list['type']==1){
	      $price = $this->getConfig()['vic_price'];
	  }else{
	      //币最新价
	      $currency = M('Currency1')->where(['currency_id'=>$list['type']])->find()['currency_mark'];
	      $url="https://api.huobi.pro/market/detail/merged?symbol=".$currency."usdt";
	      $result=$this->curl_get($url);
	      $price=$result['tick']['close'];
	  } */
	  $price = $this->getPrice()['price'.$list['type']];
	  $usd_price = $price*$list['money'];
	  $this->decTeamYeji($this->user,$usd_price);
	  $r[] = $API_USERMONEYLOGIC->setUserMoneyLogic($this->uid,$fanhuan_money, 1,'关闭智能收益返还',$list['type'],111);
	  $r[] = M('Member_money')->where(['member_id'=>$this->uid])->setInc('money'.$list['type'],$fanhuan_money);
	  $r[] = M('Project_log')->where(['id'=>$id])->delete();
	  $dongjie = M('Project_dongjie')->where(['project_id'=>$id])->find();
	  if($dongjie){
	      $r[] = M('Project_dongjie')->where(['project_id'=>$id])->delete();
	  }
	  $check = M('Project_log')->where(['uid'=>$this->uid,'type'=>$list['type']])->find();
	  if(!$check){
	      M('Project')->where(['id'=>$list['project_id']])->setField('status',0);
	  }
	  /* $project = M('Project_log')->where(['uid'=>$this->uid])->find();
	  if(!$project){
	      $r[] = M('User_relation')->where(['uid'=>$this->uid])->setField('yeji',0);
	  } */
	  //dump($r);
	   if (in_array(false, $r)){
	        $data['status']=-4;
	        $data['info']='系统繁忙';
	        $this->ajaxReturn($data);
	    }else{
	        $data['status']=1;
	        $data['info']='操作成功';
	        $this->ajaxReturn($data);
	    }
	}
	//加上线的团队业绩
	private function addTeamYeji($user,$money){
	    $uid = $user['member_id'];
	    $USERRELATION = new UserRelationModel();
	    //加团队业绩
	    $USERRELATION->addTeamYeji($uid,$money);
	    //查询上级 推荐关系
	    $USER = new MemberModel();
	    $user_relation = $USERRELATION->getUserByUid($uid);
	    if($user_relation['yeji']>10000&&$user_relation['yeji']<30000){
	        $level = 1;
	    }elseif($user_relation['yeji']>=30000&&$user_relation['yeji']<50000){
	        $level = 2;
	    }elseif($user_relation['yeji']>=50000&&$user_relation['yeji']<100000){
	        $level = 3;
	    }elseif($user_relation['yeji']>=100000&&$user_relation['yeji']<200000){
	        $level = 4;
	    }elseif($user_relation['yeji']>=200000&&$user_relation['yeji']<1000000){
	        $level = 5;
	    }elseif($user_relation['yeji']>=1000000){
	        $level = 6;
	    }else{
	        $level = '';
	    }
	    if($level){
	        M('Member')->where(['member_id'=>$uid])->setField('level',$level);
	    }
	    $up = $USER->getUserByUid($user['pid']);
	    if(!$up){
	        return;
	    }
	    $this->addTeamYeji($up,$money);
	
	}
	//减上线的团队业绩
	private function decTeamYeji($user,$money){
	    $uid = $user['member_id'];
	    $USERRELATION = new UserRelationModel();
	    //加团队业绩
	    $USERRELATION->decTeamYeji($uid,$money);
	    //查询上级 推荐关系
	    $USER = new MemberModel();
	    $up = $USER->getUserByUid($user['pid']);
	    if(!$up){
	        return;
	    }
	    $this->decTeamYeji($up,$money);
	}
    //记录 自动程序的表
	public function addProject($uid,$project_id,$money_type,$money){
	    $data['uid'] 		= $uid;
	    $data['money'] 		    = $money;
	    $data['project_id']		= $project_id;
	    $data['status']         = 1;
	    $data['add_time'] 	    = time();
	    $data['type'] 	    = $money_type;
	    $r = M('Project_log')->add($data);
	    return $r;
	}
	//今日收益
	public function todayProfit(){
		$where1['shouqu_time'] = array('gt',strtotime(date("Y-m-d"),time()));
		$where1['uid'] = $this->uid;
		$where1['status'] =1;
		$list = M('Project_auto_money')->where($where1)->sum('money');
		//echo M('Project_auto_money')->_sql();
		/* if(!$list){
			$where['add_time'] = array('gt',strtotime(date("Y-m-d"),time()));
			$where['uid'] = $this->uid;
			$where['status'] = 0;
			$list = M('Project_auto_money')->where($where)->sum('money');
		} */
	    $chanliang = M('Project_log')->where(['uid'=>$this->uid])->sum('chanliang');
	    $zongshu = M('Project_log')->where(['uid'=>$this->uid])->count();
	    $money['money'] = $list;
	    $money['chanliang'] = $chanliang?$chanliang:'0';
	    $money['zongshu'] = $zongshu?$zongshu:'0';
	    $this->ajaxReturn($money);
	}
	//代数奖
	public function runDaishuMoney($user,$money,$k,$laizi_username,$project_id,$type){
	    $project_log = M('Project_log')->where(['uid'=>$user['member_id']])->find();
	    if($project_log){
	    if($k>20){
	        return;
	    }
	    /*  $config = $this->getConfig();
	    if($k<5){
	       $bili = $config['daishu'.$k];
	    }else{
	        $bili = $config['daishu5'];
	    }  */
	    //上级直推几人
	   //$user_relation = M('User_relation')->where(['uid'=>$user['member_id']])->find();
		//if($k)
	    //dump($bili);
	    
	    //判断是不是有资格获取
	   // $user_auto_check  = $this->userAutoCheck($user['uid']);
	    //if($user_auto_check){
	        //推算应得到的动态收益
	        $money_zhitui = $money*$this->config['tuijian_bili'.$k];
	        //发钱
	        $data['uid'] = $user['member_id'];
	        $data['money'] = $money;
	        $data['laizi_username'] = $laizi_username;
	        $data['status'] = 0;
	        $data['project_id'] = $project_id;
	        $data['daishu'] = $k;
	        $data['bili'] = 0;
			 $data['type'] = $type;
	        $data['add_time'] = time();
	        M('Project_dongjie')->add($data);
	    //}
	   //直推几人可以拿几代
	       // dump($user_relation);dump($k);dump($bili);
		   	    }
				$USER = new MemberModel();
	    //本人直推关系
	    //if($user['pid']){
	    $up_user = $USER->getUserByUid($user['pid']);
		if($up_user){
	    $k++;
	    $this->runDaishuMoney($up_user,$money,$k,$laizi_username,$project_id,$type);
		}
	    //}

	}
	public function getUserMoneyByType(){
	    $type = I('type')?I('type'):3;
	    //VIC价格
	    $price = $this->getConfig()['vic_price'];
	    $exchange_fee = $this->getConfig()['exchange_fee'];
	    //$usermoney = M('Member_money')->where(['member_id'=>$this->uid])->find();
	    $project_log = M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->sum('money');
	    $list = M('Member_money')->where(['member_id'=>$this->uid])->find();
	    $return['num'] = $list['money'.$type];
	    $return['max_num'] = $project_log*0.2;
	    $return['exchange_fee'] = $exchange_fee;
	    $return['vic_price'] = $price;
	    $data['status']=1;
	    $data['data']=$return;
	    $this->ajaxReturn($data);
	}
	public function getExchangeNum(){
	    $type = I('type')?I('type'):1;
	    $num = I('num');
	    $other_type = I('other_type');
	    /* $usermoney = M('Member_money')->where(['member_id'=>$this->uid])->find();
	    //echo M('Member_money')->_sql();
	    if($usermoney['money'.$type]<$num){
	        $data['status']=-4;
	        $data['info']='余额不足';
	        $this->ajaxReturn($data);
	    } */
	    
	    if($type!=1){
	        $project_log = M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->find();
	        if(!$project_log){
	            $data['status']=-4;
	            $data['info']='您未开启AI智能收益';
	            $this->ajaxReturn($data);
	        }
    	    /* $project_log = M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->sum('money');
    	    $max_num = $project_log*0.2;
    	    if($max_num<$num){
    	        $data['status']=-4;
    	        $data['info']='最高兑换额度为量化资金的20%';
    	        $this->ajaxReturn($data);
    	    } */
	    }
	    $price = $this->getConfig()['vic_price'];
	    /* if($type==1){
            //币最新价
            $currency = M('Currency1')->where(['currency_id'=>$other_type])->find();
	    }else{
	        //币最新价
	        $currency = M('Currency1')->where(['currency_id'=>$type])->find();
	    }
	    //以太坊比特币PAI最新价
	    $url="https://api.huobi.pro/market/detail/merged?symbol=".$currency['currency_mark']."usdt";
	    $result=$this->curl_get($url);
	    $usd_price = $result['tick']['close']; */
		if($type==1){
			$usd_price = $this->getPrice()['price'.$other_type];
		}else{
			$usd_price = $this->getPrice()['price'.$type];
		}
	    
	    //兑换手续费
	    $exchange_fee = $this->getConfig()['exchange_fee'];
	    //平台币换比特币以太坊
	    if($type==1&&($other_type==2||$other_type==3||$other_type==4)){
            $exchange_num = ($price*$num)/$usd_price;
            $exchange_num = $exchange_num-($exchange_num*$exchange_fee);
	    }elseif($other_type==1&&($type==3||$type==2||$type==4)){
	        $exchange_num = ($usd_price*$num)/$price;
	    }
	    
	  //  3  1
	    $return['exchange_num'] = round($exchange_num,6);
	    $return['status']=1;
	    $this->ajaxReturn($return);
	}
	//兑换 钱包币换以太坊
	public function exchange(){
	    $num = I('num');
	    $type = I('type')?I('type'):1;
	    $other_type = I('other_type');
	    $tradepwd = I('tradepwd');
	    if($type!=1){
	        $data['status']=-1;
	        $data['info']='参数错误';
	        $this->ajaxReturn($data);
	    }
	    if($num<0){
	        $data['status']=-2;
	        $data['info']='请输入正确的数量';
	        $this->ajaxReturn($data);
	    }
	    if(md5($tradepwd)!=$this->user['pwdtrade']){
	        $data['status']=-3;
	        $data['info']='交易密码不正确';
	        $this->ajaxReturn($data);
	    }
	    $usermoney = M('Member_money')->where(['member_id'=>$this->uid])->find()['money'.$type];
	    if($usermoney<$num){
	        $data['status']=-4;
	        $data['info']='余额不足';
	        $this->ajaxReturn($data);
	    }
	    //VIC价格
	    $price = $this->getConfig()['vic_price'];
        /* $currency = M('Currency1')->where(['currency_id'=>$other_type])->find();
	    //以太坊比特币PAI最新价
	    $url="https://api.huobi.pro/market/detail/merged?symbol=".$currency['currency_mark']."usdt";
	    $result=$this->curl_get($url);
	    $usd_price = $result['tick']['close']; */
	    $usd_price = $this->getPrice()['price'.$other_type];
	    //兑换手续费
	    $exchange_fee = $this->getConfig()['exchange_fee'];
	    //平台币换比特币以太坊
        $exchange_num = ($price*$num)/$usd_price;
        $exchange_num = $exchange_num-($exchange_num*$exchange_fee);
	    $exchange_num = round($exchange_num,6);
	    //开启事物
	    M()->startTrans();
	    $r[] = $this->addExchangeLog($this->uid,$type,$exchange_num,$other_type);
	    $r[] = M('Member_money')->where(['member_id'=>$this->uid])->setInc('money'.$other_type,$exchange_num);
	    $r[] = M('Member_money')->where(['member_id'=>$this->uid])->setDec('money'.$type,$num);
	    $FINANCE_MODEL = new FinanceModel();
	    $r[] = $FINANCE_MODEL->addFinance($this->uid,$exchange_num,1,'兑换收入',$other_type,12);
	    $r[] = $FINANCE_MODEL->addFinance($this->uid,$num,2,'兑换扣除',$type,12);
	    if (in_array(false, $r)){
	        M()->rollback();
	        $data['status']=-6;
	        $data['info']='系统繁忙';
	        $this->ajaxReturn($data);
	    }else{
	        M()->commit();
	        $data['status']=1;
	        $data['info']='操作成功';
	        $this->ajaxReturn($data);
	    }
	}
	//以太坊换平台币
	public function exchangeNow(){
	    $num = I('num');
	    $type = I('type')?I('type'):1;
	    $other_type = I('other_type')?I('other_type'):1;
	    $tradepwd = I('tradepwd');
	        $data['status']=-2;
	        $data['info']='敬请期待';
	        $this->ajaxReturn($data);
	    if(!in_array($other_type||$type,array(1,2,3))){
	        $data['status']=-1;
	        $data['info']='参数错误';
	        $this->ajaxReturn($data);
	    }
	    if($num<0){
	        $data['status']=-2;
	        $data['info']='请输入正确的数量';
	        $this->ajaxReturn($data);
	    }
	    if(md5($tradepwd)!=$this->user['pwdtrade']){
	        $data['status']=-3;
	        $data['info']='交易密码不正确';
	        $this->ajaxReturn($data);
	    }
	    /* $usermoney = M('Member_money')->where(['member_id'=>$this->uid])->find()['money'.$type];
	     if($usermoney<$num){
	     $data['status']=-4;
	     $data['info']='余额不足';
	     $this->ajaxReturn($data);
	     } */
	    /* $exchange_log = M('Exchange_log')->where(['uid'=>$this->uid,'type'=>$type])->find();
	    if($exchange_log){
	        $data['status']=-4;
	        $data['info']="每个币种仅可兑换一次";
	        $this->ajaxReturn($data);
	    }
	    $project_log = M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->sum('money');
	    $max_num = $project_log*0.2;
	    if($max_num<$num){
	        $data['status']=-4;
	        $data['info']='最高兑换额度为量化资金的20%';
	        $this->ajaxReturn($data);
	    } */
		$project_log = M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->find();
	        if(!$project_log){
	            $data['status']=-4;
	            $data['info']='您未开启AI智能收益';
	            $this->ajaxReturn($data);
	        }
		$project_log = M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->sum('money');
	    if($num>$project_log){
			 $data['status']=-4;
	        $data['info']='余额不足';
	        $this->ajaxReturn($data);
		}
	    //VIC价格
	    $price = $this->getConfig()['vic_price'];
        //币最新价
        /* $currency = M('Currency1')->where(['currency_id'=>$type])->find();
	    //以太坊比特币PAI最新价
	    $url="https://api.huobi.pro/market/detail/merged?symbol=".$currency['currency_mark']."usdt";
	    $result=$this->curl_get($url);
	    $usd_price = $result['tick']['close']; */
	    $usd_price = $this->getPrice()['price'.$type];
	    //比特币以太坊换平台币
	    $exchange_num = ($usd_price*$num)/$price;
	    $exchange_num = round($exchange_num,6);
	    //开启事物
	    M()->startTrans();
		$qufen = '';
	    $r[] = $this->addExchangeLog($this->uid,$type,$exchange_num,$other_type);
	    //最后一次投入的够不够扣
	    $new_project_log = M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->find();
	    $project_log_count = M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->sum('money');
	    if($new_project_log['money']>$num){
	        M('Project_log')->where(['id'=>$new_project_log['id']])->setDec('money',$num);
	    }elseif($num==$project_log_count){
	        M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->delete();
			$qufen = 1;
	    }else{
	        $project_log = M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->select();
	        $project_log_num = M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->count();
	        $dec_money = $num/$project_log_num;
	        foreach ($project_log as $k=>$v){
	            M('Project_log')->where(['id'=>$v['id']])->setDec('money',$dec_money);
	        }
	    }
	    $r[] = M('Member_money')->where(['member_id'=>$this->uid])->setInc('money'.$other_type,$exchange_num);
	    /*  $r[] = M('Member_money')->where(['member_id'=>$this->uid])->setDec('money'.$type,$num); */
	    //自动开启智能化收益
	    $r[] = $this->autoOpen($exchange_num,1,$qufen);
	    $FINANCE_MODEL = new FinanceModel();
	    $r[] = $FINANCE_MODEL->addFinance($this->uid,$exchange_num,1,'兑换收入',$other_type,12);
	    $r[] = $FINANCE_MODEL->addFinance($this->uid,$num,2,'兑换扣除',$type,12);
	    if (in_array(false, $r)){
	        M()->rollback();
	        $data['status']=-6;
	        $data['info']='系统繁忙';
	        $this->ajaxReturn($data);
	    }else{
	        M()->commit();
	        $data['status']=1;
	        $data['info']='操作成功';
	        $this->ajaxReturn($data);
	    }
	}
	//兑换记录
	public function addExchangeLog($member_id,$type,$money,$other_type){
	    $data['uid']=$member_id;
	    $data['type']=$type;
	    $data['money']=$money;
	    $data['other_type']=$other_type;
	    $data['add_time'] = time();
	    $data['ip'] = get_client_ip();
	    $list=M('Exchange_log')->add($data);
	    if($list){
	        return $list;
	    }else{
	        return false;
	    }
	}
	//查看本人是否兑换过
	public function getExchangeLog(){
	    /* $where['uid'] = $this->uid;
	    $where['other_type'] = 1;
	    $list=M('Exchange_log')->where($where)->find();
	    $duihuan_kg = $this->getConfig()['duihuan_kg'];
	    //没有记录并且限时兑换开启才显示限时兑换按钮
	    if(!$list&&$duihuan_kg==1){ */
	        $data['status']=1;
	        $data['is_show']=1;
	        $this->ajaxReturn($data);
	    /* }else{
	        $data['status']=1;
	        $data['is_show']=-1;
	        $this->ajaxReturn($data);
	    } */
	}
	//查看本人所有兑换记录
	public function getAllExchangeLog(){
	    $where['uid'] = $this->uid;
	    $list=M('Exchange_log e')
	    ->join('join yang_currency1 c on e.other_type = c.currency_id')
	    ->where($where)
	    ->select();
	    $data['status']=1;
        $data['data']=$list;
        $this->ajaxReturn($data);
	}
	//限时兑换最大兑换数量
	public function getMaxExchangeNum(){
	    $type = I('type')?I('type'):'3';
	    $project_log = M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->find();
		$data['status']=-2;
	        $data['info']='敬请期待';
	        $this->ajaxReturn($data);
	    if(!$project_log){
	        $data['status']=-4;
	        $data['info']='您未开启AI智能收益';
	        $this->ajaxReturn($data);
	    }
	    $project_log = M('Project_log')->where(['uid'=>$this->uid,'type'=>$type])->sum('money');
	    $max_num = $project_log;
	    $data['status']=1;
	    $data['data']=$max_num;
	    $this->ajaxReturn($data);
	}
	
	
}